﻿using Abp.Application.Services;
using Abp.Configuration.Startup;
using Abp.Localization.Dictionaries;
using Abp.Localization.Dictionaries.Xml;
using Abp.Modules;
using Abp.Web.Mvc;
using Abp.WebApi;
using Swashbuckle.Application;
using System.Data.Entity;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Teleware.Newbe.Navigation.Web.Domain;
using Teleware.Newbe.Navigation.Web.Menus;
using Teleware.Newbe.Navigation.Web.Migrations;

namespace Teleware.Newbe.Navigation.Web.Modules
{
    [DependsOn(
        typeof(AbpWebApiModule),
        typeof(AbpWebMvcModule),
        typeof(NewbeAutoMapperModule),
        typeof(NewbeServiceModule))]
    public class NewbeStartupModule : AbpModule
    {
        public override void PreInitialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
            Configuration.Modules.AbpWebApi().HttpConfiguration = GlobalConfiguration.Configuration;
            Configuration.Navigation.Providers.Add<DefaultNavigationProvider>();
            Configuration.Auditing.IsEnabledForAnonymousUsers = true;
            Configuration.Modules.AbpWebCommon().SendAllExceptionsToClients = true;
        }

        public override void Initialize()
        {
            var abpWebApiConfiguration = Configuration.Modules.AbpWebApi();
            abpWebApiConfiguration.DynamicApiControllerBuilder
                .ForAll<IApplicationService>(Assembly.GetAssembly(typeof(NewbeStartupModule)), "app")
                .Build();
            var httpConfiguration = abpWebApiConfiguration.HttpConfiguration;
            ConfigureSwaggerUi();
            httpConfiguration.MapHttpAttributeRoutes();
            httpConfiguration.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            Configuration.Localization.Sources.Add(
                new DictionaryBasedLocalizationSource(
                    "Teleware.Newbe.Navigation.Web",
                    new XmlFileLocalizationDictionaryProvider(
                        HttpContext.Current.Server.MapPath("~/Localization/Teleware.Newbe.Navigation.Web")
                    )
                )
            );

            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            Database.SetInitializer(new MigrateDatabaseToLatestVersion<NewbeDbContext, Configuration>());
        }

        private void ConfigureSwaggerUi()
        {
            Configuration.Modules.AbpWebApi().HttpConfiguration
                .EnableSwagger(c =>
                {
                    c.SingleApiVersion("v1", "Teleware.Newbe.Navigation.WebApi");
                    c.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
                })
                .EnableSwaggerUi(c =>
                {
                    c.InjectJavaScript(Assembly.GetAssembly(typeof(NewbeStartupModule)),
                        "Teleware.Newbe.Navigation.Web.Scripts.Swagger-Custom.js");
                });
        }
    }
}
